package com.sk.ccflower.server.common.xmlRpcManager;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.XmlRpcRequest;
import org.apache.xmlrpc.server.RequestProcessorFactoryFactory.RequestSpecificProcessorFactoryFactory;

import com.sk.ccflower.server.lib.dbUtils.ConnectionPoolManager;

public class MyRequestSpecificProcessorFactoryFactory extends RequestSpecificProcessorFactoryFactory {
	
	private ConnectionPoolManager mConnectionPool = null;
	
	public MyRequestSpecificProcessorFactoryFactory( ConnectionPoolManager _dbcp ) {
		this.mConnectionPool = _dbcp;
	}
	
	@Override
	public RequestProcessorFactory getRequestProcessorFactory(final Class pClass) throws XmlRpcException {
        return new RequestProcessorFactory() {
            public Object getRequestProcessor(XmlRpcRequest pRequest) throws XmlRpcException {
                return MyRequestSpecificProcessorFactoryFactory.this.getRequestProcessor(pClass, pRequest);
            }
        };
    }
	
	@Override
	protected Object getRequestProcessor(Class pClass, XmlRpcRequest pRequest) throws XmlRpcException {
		Constructor<?> cstr;
		try {
			cstr = pClass.getConstructor( ConnectionPoolManager.class );
			return cstr.newInstance( this.mConnectionPool );
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
